<?php

namespace Modules\Common\Models;

use Catch\Exceptions\FailedException;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Schema;

class Area extends Model
{
    protected $table = 'areas';
    protected $timestamp = false;
    public function getAll()
    {
        if (!Schema::hasTable('areas')) {
            throw new FailedException('请使用 php artisan catch:areas 获取地区数据源');
        }
        $json = file_get_contents(storage_path('/region.json'));
        
        $list = json_decode($json,true);
        $data['code'] = 1;
        $data['msg'] ='';
        $data['data'] = $list;
        return  $data;
        return $this->whereIn('level', [1,2,3,4])->get(['id', 'parent_id', 'name'])->toTree(0, 'parent_id')
            ->filter(function ($area) {
                return isset($area['children']);
            })->values();
    }
}
